package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.atguigu.gmall.product.service.BaseCategoryTradeMarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: BaseCategoryTradeMarkServiceImpl
 * Package: com.atguigu.gmall.product.service.impl
 * Description:
 *
 * @Author 刘轩
 * @Create 2023-08-18 22:24
 * @Version 1.0
 */

@Service
public class BaseCategoryTradeMarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTradeMarkService {
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;
    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    /**
     * 根据三级分类Id 获取到可选品牌列表。
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        // 查询根据三级分类查询品牌id
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(trademarkLambdaQueryWrapper);
        //List<Long> tmIdLIst = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> baseCategoryTrademark.getTrademarkId()).collect(Collectors.toList());
        // 使用方法引用获取数据，SELECT  trademark_id from base_category_trademark WHERE category3_id=61 and is_deleted=0
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            // 查询品牌id

            List<Long> trademarkList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            return baseTrademarkMapper.selectBatchIds(trademarkList);
        }
        return null;
    }

    /**
     * 根据三级分类Id获取到可选品牌列表
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> trademarkList = baseCategoryTrademarkMapper.selectList(trademarkLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(trademarkList)) {
            // 查询已绑定的品牌id
            List<Long> tmIdList = trademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            // A 全集
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null);
//            ArrayList<BaseTrademark> baseTradeMarkArrayList = new ArrayList<>();
//            if (!CollectionUtils.isEmpty(baseTrademarkList)) {
//                baseTrademarkList.forEach((baseTrademark -> {
//                    if (!tmIdList.contains(baseTrademark.getId())) {
//                        baseTradeMarkArrayList.add(baseTrademark);
//                    }
//                }));
//            }
            if (!CollectionUtils.isEmpty(baseTrademarkList)) {
                List<BaseTrademark> baseTrademarkArrayList = baseTrademarkList.stream().filter(baseTrademark -> {
//                    if (!tmIdList.contains(baseTrademark.getId())) {
//                        return baseTrademark;
//                    }
                    // 获取差集
                    return !tmIdList.contains(baseTrademark.getId());
                }).collect(Collectors.toList());
                return baseTrademarkArrayList;
            }
        }
        return baseTrademarkMapper.selectList(null);
    }

    /**
     * 保存分类Id 与 品牌的关系
     *
     * @param categoryTrademarkVo
     */
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        // base_category_trademark
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            // 循环遍历
            trademarkIdList.forEach(tmId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(tmId);
                // 循环插入数据
                baseCategoryTrademarkMapper.insert(baseCategoryTrademark);

            });
        }
    }

    /**
     * 删除品牌与分类的关系
     *
     * @param category3Id
     * @param trademarkId
     */
    @Override
    public void remove(Long category3Id, Long trademarkId) {
// 删除本质就是逻辑删除 跟新数据
        LambdaQueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseCategoryTrademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        baseCategoryTrademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        baseCategoryTrademarkMapper.delete(baseCategoryTrademarkLambdaQueryWrapper);
    }
}

